﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

using Aliphant.Common;
using Aliphant.Mvvm.XEvent.Events;
using Aliphant.Mvvm.XEvent.UI;
using Aliphant.Widget.Calendar;
using Aliphant.Widget.Calendar.Models;

using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Aliphant.Widget.Calendar.ViewModels
{
    public class WidgetCalendarViewModel : BindableBase
    {
        private WindowEvent _windowEvent;
        private Timer _timer;
        private DateTime _today;
        private int _count = 0;

        public WidgetCalendarViewModel()
        {
            _windowEvent = CalenderModule.EventAgent.GetEvent<WindowEvent>();
            _windowEvent.Subscribe(Release, (p) => p.Type == EventType.Action && p.WindowActionType == WindowActionType.Close && p.EventId == EventIds.Release);

            // 创建定时器并启动
            _timer = new Timer((e) =>
            {
                _count++;
                //Debug.WriteLine(_count);
                GetMonth(DateTime.Today);
            }, null, 1000, 1000);

        }

        public ObservableCollection<CalenderDay> DatesCollection { get; private set; } = new();

        /// <summary>
        /// 获取日期所在的月份信息
        /// </summary>
        /// <param name="date">日期</param>
        private void GetMonth(DateTime date)
        {
            if (date == _today)
            {
                return;
            }

            _today = date;
            CalenderMonth cMonth = new CalenderMonth(date);
            CreateMonthDays(cMonth, date.Day);
        }

        private void CreateMonthDays(CalenderMonth cMonth, int day)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                DatesCollection.Clear();
            });

            int coverCount = cMonth.FirstDayWeek - 1;

            // 用上个月的日期填充
            int month = (cMonth.Month + 10) % 12 + 1;
            int days = DateTime.DaysInMonth(cMonth.Year, month);

            Application.Current.Dispatcher.Invoke(() =>
            {
                for (int i = 0; i < coverCount; i++)
                {
                    DatesCollection.Insert(0, new CalenderDay() { Day = days, LastMonth = true });
                    days--;
                }

                for (int i = 1; i <= cMonth.Days; i++)
                {
                    CalenderDay cDay = new CalenderDay();
                    cDay.Day = i;
                    cDay.Week = (cMonth.FirstDayWeek + i + 5) % 7 + 1;
                    cDay.Month = cMonth.Month;
                    cDay.Year = cMonth.Year;

                    if (i == day)
                    {
                        cDay.Today = true;
                    }
                    if (i == 1)
                    {
                        cDay.FirstDay = true;
                    }
                    if (i == cMonth.Days)
                    {
                        cDay.LastDay = true;
                    }
                    DatesCollection.Add(cDay);
                }
            });
        }

        private void Release(WindowEvent @event)
        {
            _timer?.Dispose();
        }


    }
}
